﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA.UserControls.Tools
{
    public partial class UcHolidays : BaseUserControl
    {
        #region Local Objects
        BusinessEntity.Configuration.Holidays holidays;
        #endregion

        #region Starting Control
        public UcHolidays()
        {
            InitializeComponent();
        }
        private void UcHolidays_Load(object sender, EventArgs e)
        {
            StartControl();
        }
        private void StartControl()
        {
            BusinessLogic.Configuration blconfig = new BusinessLogic.Configuration();
            holidays = blconfig.GetHolidays();
            LoadCurrentHolidays();
        }

        #endregion

        #region Events
        private void cmdInsert_Click(object sender, EventArgs e)
        {
            InsertHoliday();
        }
        private void cmdRemove_Click(object sender, EventArgs e)
        {
            RemoveHoliday();
        }
        private void lstHolidays_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
                RemoveHoliday();
        }
        private void lstHolidays_Resize(object sender, EventArgs e)
        {
            lstHolidays.Columns[0].Width = lstHolidays.Width - 20;
        }
        private void removerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveHoliday();
        }
        private void imprimirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintHolidayList();
        }
        private void exportarXLSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportHolidayListToXLS();
        }
        private void cmdPrint_Click(object sender, EventArgs e)
        {
            PrintHolidayList();
        }
        private void cmdExportXLS_Click(object sender, EventArgs e)
        {
            ExportHolidayListToXLS();
        }
        #endregion

        #region Methods
        private void LoadCurrentHolidays()
        {
            lstHolidays.Items.Clear();
            if (holidays != null && holidays.Holiday != null && holidays.Holiday.Count > 0)
            {
                foreach (DateTime item in holidays.Holiday)
                {
                    ListViewItem lstItem = new ListViewItem(item.ToString("dd/MM/yyyy"));
                    lstItem.Tag = item;
                    lstHolidays.Items.Add(lstItem);
                }
            }
        }
        internal GTA.BusinessEntity.Configuration.Holidays GetHolidaysConfiguration()
        {
            return holidays;
        }
        private void InsertHoliday()
        {
            if (dtHolidayDate.Value == null || dtHolidayDate.Value == DateTime.MinValue)
                MessageBox.Show("Selecione a data", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else
            {
                CheckHolidayObjectInstance();
                if (!CheckExistingHoliday(dtHolidayDate.Value))
                {
                    holidays.Holiday.Add(dtHolidayDate.Value);
                    LoadCurrentHolidays();
                }
                else
                    MessageBox.Show("O feriado já foi adicinoado!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        private bool CheckExistingHoliday(DateTime date)
        {
            return holidays.Holiday.Exists(item => item.Date.CompareTo(date.Date) == 0);
        }
        private void CheckHolidayObjectInstance()
        {
            if (holidays == null)
                holidays = new GTA.BusinessEntity.Configuration.Holidays();
            if (holidays.Holiday == null)
                holidays.Holiday = new List<DateTime>();
        }
        private void RemoveHoliday()
        {
            if (lstHolidays.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Deseja realmente excluir os feriados selecionados?", "Questão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    while (lstHolidays.SelectedItems.Count > 0)
                    {
                        holidays.Holiday.RemoveAll(item => item.CompareTo((DateTime)lstHolidays.SelectedItems[0].Tag) == 0);
                        lstHolidays.Items.Remove(lstHolidays.SelectedItems[0]);
                    }
                }
            }
            else
                MessageBox.Show("Selecione ao menos uma data para remover!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        private void PrintHolidayList()
        {
            try
            {
                if (holidays != null && holidays.Holiday != null && holidays.Holiday.Count > 0)
                {
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    String htmlContent = bl.GetHolidaysHTMLFormat(holidays);
                    FrmPrint wd = new FrmPrint(htmlContent);
                    wd.Show(this);
                }
            }
            catch (Exception ex)
            {
                HandleException("Imprimir resultado da pesquisa", ex);
            }
        }
        private void ExportHolidayListToXLS()
        {
            try
            {
                if (holidays != null && holidays.Holiday != null && holidays.Holiday.Count > 0)
                {
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        String pathXlsFile = saveFileDialog.FileName;
                        BusinessLogic.GTA bl = new BusinessLogic.GTA();
                        bl.ExportHolidays(holidays, pathXlsFile);
                        if (MessageBox.Show("Arquivo salvo com sucesso.\r\n\r\nDeseja abrir o arquivo!?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(pathXlsFile);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Exportar resultado da pesquisa para XLS", ex);
            }
        }
        #endregion
    }
}
